﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace csexplorer.UserControls
{
    public partial class TreeViewExplorer : UserControl
    {
        public delegate void FileSelectedHandler(object sender, FileSelectedArg e);

        public event FileSelectedHandler FileSelected;

        private CsExplorerManager explorerMgr;

        public CsExplorerManager ExplorerMgr
        {
            get { return explorerMgr; }
            set { explorerMgr = value; }
        }

        public TreeViewExplorer()
        {
            InitializeComponent();
        }

        public void OnFileSelected(DiskEntity dskEnt)
        {
            if(FileSelected != null)
                FileSelected(this, new FileSelectedArg(dskEnt));
        }

        private void TreeViewExplorer_Load(object sender, EventArgs e)
        {
            PopulateRoot();
        }

        private void treeViewFolders_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //PopulateFolder(e.Node);           
        }

        private void treeViewFolders_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if(e.Node.Nodes.Count == 1 && e.Node.Nodes[0].Text == "_")
                PopulateFolder(e.Node);
        }

        private void treeViewFolders_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag is DriveInformation || e.Node.Tag is DirectoryInformation)
            {
                PopulateFolder(e.Node);

                if (!e.Node.IsExpanded)
                    e.Node.Expand();
            }

            OnFileSelected(e.Node.Tag as DiskEntity);
        }

        private void PopulateRoot()
        {
            if (explorerMgr == null)
                return;

            List<DiskEntity> entities = explorerMgr.GetRoodFolderContent();

            foreach (DiskEntity dskEnt in entities)
            {
                int imgIndex = GetImgIndex(dskEnt, false);
                int imgSelIndex = GetImgIndex(dskEnt, true);
                TreeNode newNode = new TreeNode(dskEnt.Name, imgIndex, imgSelIndex);
                newNode.Tag = dskEnt;
                newNode.Nodes.Add("_");
                treeViewFolders.Nodes.Add(newNode);
            }
        }

        private void PopulateFolder(TreeNode selectedNode)
        {
            this.Cursor = Cursors.WaitCursor;

            //clear all sub-folders
            selectedNode.Nodes.Clear();

            string selFolder = ((DiskEntity)selectedNode.Tag).Path;
            
            // Fill folders
            List<DirectoryInformation> foldersInfo = explorerMgr.GetFolders(selFolder);

            foreach (DirectoryInformation dskEnt in foldersInfo)
            {
                TreeNode newNode = new TreeNode();
                newNode.Text = dskEnt.Name;
                newNode.ImageIndex = GetImgIndex(dskEnt, false);
                newNode.SelectedImageIndex = GetImgIndex(dskEnt, true);
                newNode.Tag = dskEnt;
                selectedNode.Nodes.Add(newNode);
            }

            // Fill files
            List<FileInformation> filesInfo = explorerMgr.GetFiles(selFolder);
            filesInfo = explorerMgr.GetFiles(selFolder);

            foreach (DiskEntity dskEnt in filesInfo)
            {
                TreeNode newNode = new TreeNode();
                newNode.Text = dskEnt.Name;
                newNode.ImageIndex = GetImgIndex(dskEnt, false);
                newNode.SelectedImageIndex = GetImgIndex(dskEnt, true);
                newNode.Tag = dskEnt;
                selectedNode.Nodes.Add(newNode);
            }

            this.Cursor = Cursors.Default;
        }

        private int GetImgIndex(DiskEntity entity, bool isSelected)
        {
            int imgIndex = 0;

            if (entity is DriveInformation)
            {
                if (((DriveInformation)entity).DriveType == DriveType.CD)
                {
                    imgIndex = 1;
                }
                else if (((DriveInformation)entity).DriveType == DriveType.Network)
                {
                    imgIndex = 2;
                }
                else if (((DriveInformation)entity).DriveType == DriveType.Removable)
                {
                    imgIndex = 3;
                }
            }
            else if (entity is DirectoryInformation)
            {
                imgIndex = (isSelected) ? 5 : 4;
            }
            else if (entity is FileInformation)
            {
                imgIndex = 6;
            }

            return imgIndex;
        }
    }
}
